<?php


namespace App\Http\Repositories\Admin\Base;


use App\Http\Repositories\BaseRepository;
use App\Models\Base\CategoryModel;

class CategoryRepository extends BaseRepository
{
    public $category;
    public function __construct(CategoryModel $category)
    {
        $this->category = $category;
    }

    /**
     * 添加
     * @param $data
     * @return mixed
     */
    public function addCategory($data)
    {
        $data = $this->category->setUserInfo($data);

        return $this->category->create($data);
    }

    /**
     * 获取详情
     * @param $data
     * @return mixed
     * @throws \App\Exceptions\ApiException
     */
    public function getCategoryInfo($data)
    {
        return $this->category->getInfo($data);
    }

    /**
     * 获取列表
     * @param $data
     * @return mixed
     */
    public function getCategoryList($data)
    {
        $res = $this->category->get()->toArray();

        if ($data['type'] == 1) {
            return $this->generateTree($res);
        } elseif($data['type'] == 2) {
            return $res;
        } else {
            return $this->generateTreeStr($res);
        }

    }

    /**
     * 删除
     * @param $data
     * @return array|string[]
     */
    public function delCategory($data)
    {
        return $this->category->deleteItem($data);
    }

    public function editCategory($data)
    {
        $where['id'] = $data['id'];

        return $this->category->editItem($where, $data);
    }
}
